package com.xhd.db;

import java.lang.reflect.Field;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.xBaseObj;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.config.BizObjConfig;
import com.xhd.exception.DatabaseException;
import com.xhd.exception.SystemException;
import com.xhd.util.DataFormat;

public class BizObjPK implements UseCache {
	private String[] jm155;
	private String[] jm156;
	private static Hashtable jm157 = new Hashtable(512);
	private static final Log jm9;
	private static boolean jm158;
	static Class jm10;

	static {
		Class tmp16_13 = jm10;
		if (tmp16_13 == null) {

			try {
				BizObjPK.jm10 = Class
						.forName("com.sinocc.base.config.BizObjPK");
			} catch (ClassNotFoundException localClassNotFoundException) {
				throw new NoClassDefFoundError(localClassNotFoundException
						.getMessage());
			}
		}
		jm9 = LogFactory.getLog(tmp16_13);
		jm158 = false;
	}

	public static BizObjPK getBizObjPK(xBaseObj paramxBaseObj)
			throws SystemException {
		if (paramxBaseObj == null)
			throw new SystemException(10062);
		BizObjPK localBizObjPK = findBizObjPK(paramxBaseObj.getClass());
		if (localBizObjPK == null)
			throw new SystemException(10061, paramxBaseObj.getClass().getName());
		return localBizObjPK;
	}

	public static BizObjPK getDBObjPK(xBaseObj paramxBaseObj)
			throws DatabaseException {
		if (paramxBaseObj == null)
			throw new DatabaseException(10062);
		BizObjPK localBizObjPK = findBizObjPK(paramxBaseObj.getClass());
		if (localBizObjPK == null)
			throw new DatabaseException(10061, paramxBaseObj.getClass()
					.getName());
		return localBizObjPK;
	}

	public static BizObjPK getInstance() {
		return new BizObjPK();
	}

	private BizObjPK() {
		if (jm158)
			return;
		jm158 = true;
		CacheMgr.registerCache(this);
	}

	private static String jm154(Class paramClass) {
		String str = "";
		if (paramClass != null) {
			str = paramClass.getName();
			if (str != null) {
				int i = str.lastIndexOf(46);
				if (i >= 0)
					str = str.substring(i + 1);
				str = str.toLowerCase();
			}
		}
		return str;
	}

	public static BizObjPK findBizObjPK(Class paramClass) {
		String str1 = jm154(paramClass);
		BizObjPK localBizObjPK = (BizObjPK) jm157.get(str1);
		if (localBizObjPK == null) {
			localBizObjPK = new BizObjPK();
			String str2 = BizObjConfig.findPKField(str1);
			if ((str2 == null) || (str2.length() < 1))
				return null;
			String[] arrayOfString1 = DataFormat.splitString(
					str2.toLowerCase(), ',');
			String[] arrayOfString2 = new String[arrayOfString1.length];
			Field[] arrayOfField = paramClass.getFields();
			for (int i = 0; i < arrayOfString1.length; ++i)
				for (int j = 0; j < arrayOfField.length; ++j) {
					if (!(arrayOfString1[i].equals(arrayOfField[j].getName()
							.toLowerCase())))
						continue;
					arrayOfString1[i] = arrayOfField[j].getName();
					arrayOfString2[i] = jm154(arrayOfField[j].getType());
					break;
				}
			localBizObjPK.jm155 = arrayOfString1;
			localBizObjPK.jm156 = arrayOfString2;
			jm157.put(str1, localBizObjPK);
		}
		return localBizObjPK;
	}

	public String getPKField() {
		if (this.jm155 == null)
			return null;
		String str = this.jm155[0];
		if (this.jm155.length > 1)
			for (int i = 1; i < this.jm155.length; ++i)
				str = str + "," + this.jm155[i];
		return str;
	}

	public Vector getPKFields() {
		Vector localVector = new Vector(this.jm155.length);
		for (int i = 0; i < this.jm155.length; ++i)
			localVector.add(this.jm155[i]);
		return localVector;
	}

	public String[] getPKValue(Object paramObject) {
		if (this.jm155 == null)
			return null;
		String[] arrayOfString = new String[this.jm155.length];
		for (int i = 0; i < this.jm155.length; ++i) {
			Object localObject = null;
			try {
				Field localField = paramObject.getClass().getField(
						this.jm155[i]);
				localObject = localField.get(paramObject);
			} catch (Exception localException) {
				jm9.error(localException);
				arrayOfString[i] = null;
			}
			if (localObject != null)
				arrayOfString[i] = localObject.toString();
			else
				arrayOfString[i] = null;
		}
		return arrayOfString;
	}

	public String[] getPKType() {
		return this.jm156;
	}

	public boolean isString() {
		if (this.jm156 != null)
			return this.jm156[0].equalsIgnoreCase("string");
		return false;
	}

	public String getSQLWhere(Object paramObject) {
		String[] arrayOfString = getPKValue(paramObject);
		if (arrayOfString == null)
			return null;
		StringBuffer localStringBuffer = new StringBuffer(64);
		for (int i = 0; i < this.jm155.length; ++i) {
			if (i > 0)
				localStringBuffer.append(" and ");
			localStringBuffer.append(this.jm155[i]);
			if (this.jm156[i].equals("string"))
				if (arrayOfString[i] == null) {
					localStringBuffer.append("=''");
				} else {
					localStringBuffer.append("='");
					localStringBuffer.append(DBUtil
							.filterStringToDB(arrayOfString[i]));
					localStringBuffer.append("'");
				}
			else if (arrayOfString[i] == null)
				localStringBuffer.append("=0");
			else
				localStringBuffer.append("=" + arrayOfString[i]);
		}
		return localStringBuffer.toString();
	}

	public String getSQLWhere(Map paramMap) {
		if (this.jm155 == null)
			return null;
		String[] arrayOfString = new String[this.jm155.length];
		for (int i = 0; i < this.jm155.length; ++i)
			arrayOfString[i] = ((String) paramMap.get(this.jm155[i]
					.toLowerCase()));
		StringBuffer localStringBuffer = new StringBuffer(64);
		for (int j = 0; j < this.jm155.length; ++j) {
			if (j > 0)
				localStringBuffer.append(" and ");
			localStringBuffer.append(this.jm155[j]);
			if (this.jm156[j].equals("string"))
				if (arrayOfString[j] == null) {
					localStringBuffer.append("=''");
				} else {
					localStringBuffer.append("='");
					localStringBuffer.append(DBUtil
							.filterStringToDB(arrayOfString[j]));
					localStringBuffer.append("'");
				}
			else if (arrayOfString[j] == null)
				localStringBuffer.append("=0");
			else
				localStringBuffer.append("=" + arrayOfString[j]);
		}
		return localStringBuffer.toString();
	}

	public DBWhereExp getWhereExp(Object paramObject) {
		String[] arrayOfString = getPKValue(paramObject);
		if (arrayOfString == null)
			return null;
		Object localObject = null;
		for (int i = 0; i < this.jm155.length; ++i) {
			DBWhereExp localDBWhereExp = null;
			if (this.jm156[i].equals("string"))
				if (arrayOfString[i] == null)
					localDBWhereExp = DBWhereExp.eq(this.jm155[i], "");
				else
					localDBWhereExp = DBWhereExp.eq(this.jm155[i],
							arrayOfString[i]);
			else if (arrayOfString[i] == null)
				localDBWhereExp = DBWhereExp.eq(this.jm155[i], 0);
			else
				localDBWhereExp = DBWhereExp.eq(this.jm155[i], Integer
						.parseInt(arrayOfString[i]));
			if (localObject == null)
				localObject = localDBWhereExp;
			else
				((DBWhereExp) localObject).and(localDBWhereExp);
		}
		return (DBWhereExp) localObject;
	}

	public DBWhereExp getWhereExp(Map paramMap) {
		if (this.jm155 == null)
			return null;
		String[] arrayOfString = new String[this.jm155.length];
		for (int i = 0; i < this.jm155.length; ++i)
			arrayOfString[i] = ((String) paramMap.get(this.jm155[i]
					.toLowerCase()));
		Object localObject = null;
		for (int j = 0; j < this.jm155.length; ++j) {
			DBWhereExp localDBWhereExp = null;
			if (this.jm156[j].equals("string"))
				if (arrayOfString[j] == null)
					localDBWhereExp = DBWhereExp.eq(this.jm155[j], "");
				else
					localDBWhereExp = DBWhereExp.eq(this.jm155[j],
							arrayOfString[j]);
			else if (arrayOfString[j] == null)
				localDBWhereExp = DBWhereExp.eq(this.jm155[j], 0);
			else
				localDBWhereExp = DBWhereExp.eq(this.jm155[j], Integer
						.parseInt(arrayOfString[j]));
			if (localObject == null)
				localObject = localDBWhereExp;
			else
				((DBWhereExp) localObject).and(localDBWhereExp);
		}
		return (DBWhereExp) localObject;
	}

	public boolean clearCache() {
		jm157.clear();
		return true;
	}

	public int cacheSize() {
		return jm157.size();
	}

	public Object cacheData() {
		return jm157;
	}
}
